package com.xianyun.book.gatewayimpl;

import com.alibaba.cola.exception.Assert;
import com.xianyun.book.common.util.OrderGenerator;
import com.xianyun.book.common.util.RandomUtils;
import com.xianyun.book.domain.gateway.PayGateway;
import com.xianyun.book.domain.pay.PayStatus;
import com.xianyun.book.gatewayimpl.database.OrderMapper;
import com.xianyun.book.gatewayimpl.database.PayOrderMapper;
import com.xianyun.book.gatewayimpl.database.dataobject.OrderDO;
import com.xianyun.book.gatewayimpl.database.dataobject.PayOrderDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ytt
 */
@Slf4j
@Component
public class PayGatewayImpl implements PayGateway {
    @Resource
    private PayOrderMapper payMapper;

    @Resource
    private OrderMapper orderMapper;

    @Override
    public Map<String, Object> createOrderPaymentAndGetInfo(String orderId) {
        Map<String,Object> resultMap = new HashMap<>(2);
        OrderDO infoOrder = orderMapper.getOrderInfoById(orderId);
        Assert.notNull(infoOrder,"订单不存在!请确认订单ID是否正确!");
        PayOrderDO payOrder = new PayOrderDO();
        payOrder.setOrderId(orderId);
        payOrder.setPayMoney(infoOrder.getTotalPrice());
        payOrder.setPayId(OrderGenerator.generateOrder());
        payOrder.setSerialNumber(RandomUtils.getNo(10));
        payOrder.setPayStatus(PayStatus.WAIT_CONFIRM);
        payMapper.save(payOrder);
        resultMap.put("payId",payOrder.getPayId());
        resultMap.put("totalMoney",infoOrder.getTotalPrice());
        return resultMap;
    }

    @Override
    public void finishPayment(String payId) {

    }
}
